﻿using System;
using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System.Data;
using System.Text;

namespace msBsc.AppLogic.DataAlc
{
    public class CBizDjkOrdersVehicle : CustBizObject
    {
        private CDmDjkOrdersVehicle m_CDmDjkOrdersVehicle = new CDmDjkOrdersVehicle();
        private CDmDjkOrdersVehicle oDmDjkOrdersVehicle
        {
            get { return m_CDmDjkOrdersVehicle; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizDjkOrdersVehicle()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmDjkOrdersVehicle.DJK_ORDERS_VEHICLE);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["ORDER_VEHICLE_ID"];
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }

            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            return true;
        }
        protected override bool BeforeRowDeletedR(DataRow drw)
        {
            //using (msBsc.AppLogic.DataAlc.CBizShpPageColumn objtem = new CBizShpPageColumn())
            //{
            //    DataSet ds = objtem.SelectByPageNo(this.GetRowStr(drw, KeyFieldName));
            //    for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
            //    {
            //        ds.Tables[0].Rows[i].Delete();
            //    }
            //    objtem.Update(ds);
            //} 
            return true;
        }

        protected override bool AfterRowAdded(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowDeletedR(DataRow drw)
        {
            return true;
        }

        //public DataSet SelectByIds(string strIdS)
        //{
        //    CDbParmCollection parmCollection = new CDbParmCollection();
        //    parmCollection.Add(new CDbParm("strIdS", ParameterDirection.Input, EDbType.String, 36, strIdS.Replace("'", "").Replace(",", "','")));
        //    DataSet ds = this.DataObj.QueryBySQL("SELECT * FROM " + this.TableName + " WHERE  " + this.KeyFieldName + " IN  ('@strIdS')");
        //    ds.Tables[0].TableName = this.TableName;
        //    return ds;
        //}

        /// <summary>
        /// 添加订单明细车辆信息
        /// </summary>
        /// <param name="orderId">订单编号</param>
        /// <param name="vehiclesInfo">车辆信息(格式要求：车辆一编号,车辆一数量|车辆二编号,车辆二数量|...)</param>
        /// <returns></returns>
        public string[] AddOrderVehicle(string orderId, string vehiclesInfo)
        {
            string solderId = string.Empty;
            decimal totalBalance = 0;
            string sqlString = "SELECT * FROM DJK_VEHICLE_INFO WHERE 1<>1";

            DataSet ds = new CDmDjkOrdersVehicle();

            if (vehiclesInfo.IndexOf('|') == -1)
            {
                string[] temp = vehiclesInfo.Split(',');
                sqlString += " OR VEHICLE_INFO_ID = '" + temp[0] + "'";
                DataSet ds1 = this.DataObj.QueryBySQL(sqlString);
                if (ds1.Tables[0].Rows.Count <= 0)
                {
                    return new string[] { "ERROR", totalBalance.ToString() };
                }

                solderId = ds1.Tables[0].Rows[0]["DJACK_ID"].ToString();

                DataRow dr = GetDataRow(orderId, int.Parse(temp[1]), ds1.Tables[0].Rows[0], ds.Tables[0].NewRow());

                totalBalance += Convert.ToDecimal(dr["ORDER_VEHICLE_PRICE"]) * int.Parse(temp[1]);

                ds.Tables[0].Rows.Add(dr);
            }
            else
            {
                string[] tempParm = vehiclesInfo.Split('|');

                for (int i = tempParm.Length - 1; i >= 0; i--)
                {
                    string[] tempVehicle = tempParm[i].Split(',');

                    sqlString += " OR VEHICLE_INFO_ID = '" + tempVehicle[0] + "'";
                    DataSet ds1 = this.DataObj.QueryBySQL(sqlString);
                    if (ds1.Tables[0].Rows.Count <= 0)
                    {
                        return new string[] { "ERROR", totalBalance.ToString() };
                    }

                    solderId = ds1.Tables[0].Rows[0]["DJACK_ID"].ToString();

                    DataRow dr = GetDataRow(orderId, int.Parse(tempVehicle[1]), ds1.Tables[0].Rows[0], ds.Tables[0].NewRow());

                    totalBalance += Convert.ToDecimal(dr["ORDER_VEHICLE_PRICE"]) * int.Parse(tempVehicle[1]);

                    ds.Tables[0].Rows.Add(dr);
                }
            }

            this.Update(ds);

            return new string[] { solderId, totalBalance.ToString() };
        }

        /// <summary>
        /// 获取数据行
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="vehicleNum"></param>
        /// <param name="sourceRow"></param>
        /// <param name="destinRow"></param>
        /// <returns></returns>
        private DataRow GetDataRow(string orderId, int vehicleNum, DataRow sourceRow, DataRow destinRow)
        {
            destinRow["CUSTOMER_ORDER_ID"] = orderId;
            destinRow["VEHICLE_ID"] = sourceRow["VEHICLE_INFO_ID"];
            destinRow["ORDER_VEHICLE_NAME"] = sourceRow["VEHICLE_NAME"];
            destinRow["ORDER_VEHICLE_PIC"] = sourceRow["VEHICLE_DEFAULT_PIC"];
            destinRow["ORDER_VEHICLE_CODE"] = sourceRow["VEHICLE_CODE"];
            destinRow["ORDER_VEHICLE_RMK"] = sourceRow["VEHICLE_RMK"];
            destinRow["ORDER_VEHICLE_PRICE"] = sourceRow["VEHICLE_PRICE_DAY"];
            destinRow["MODIFIED_PRICE"] = sourceRow["VEHICLE_PRICE_DAY"];
            destinRow["ORDER_VEHICLE_NUM"] = vehicleNum;
            destinRow["ORDER_VEHICLE_ID"] = this.DataObj.NewID();

            return destinRow;
        }

        /// <summary>
        /// 根据订单编号查询订单车辆信息
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public DataSet GetVehicleByOrderId(string orderId)
        {
            if (string.IsNullOrEmpty(orderId))
            {
                return null;
            }
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("orderid", ParameterDirection.Input, EDbType.String, 36, orderId.Replace("'", "").Replace(",", "','")));

            DataSet ds = this.DataObj.QueryBySQL(@"SELECT A.ORDER_VEHICLE_ID,A.ORDER_VEHICLE_NAME,A.ORDER_VEHICLE_PIC,A.ORDER_VEHICLE_CODE,A.ORDER_VEHICLE_PRICE,A.ORDER_VEHICLE_NUM,A.MODIFIED_PRICE FROM DJK_ORDERS_VEHICLE A WHERE A.CUSTOMER_ORDER_ID=@orderid", parmCollection);

            return ds;
        }
    }
}
